import '../entity/k_entity.dart';

class KLineEntity extends KEntity {
  late double open;
  late double high;
  late double low;
  late double close;
  late double vol;

  ///以基础币种计量的交易量
  late double amount;

  ///交易次数
  late int count;

  ///时间戳，单位秒，并以此作为此K线柱的id
  late int id;

  KLineEntity({
    required this.id,
    required this.open,
    required this.high,
    required this.low,
    required this.close,
    required this.vol,
    required this.amount,
    required this.count,
    required num change,
    required num changePercent,
  }) {
    super.change = change;
    super.changePercent = changePercent;
  }

  KLineEntity.fromJson(Map<String, dynamic> json) {
    open = (num.tryParse(json['open']?.toString() ?? '0') ?? 0).toDouble();
    high = (num.tryParse(json['high']?.toString() ?? '0') ?? 0).toDouble();
    low = (num.tryParse(json['low']?.toString() ?? '0') ?? 0).toDouble();
    close = (num.tryParse(json['close']?.toString() ?? '0') ?? 0).toDouble();
    vol = (num.tryParse(json['vol']?.toString() ?? '0') ?? 0).toDouble();
    amount = (num.tryParse(json['amount']?.toString() ?? '0') ?? 0).toDouble();
    count = (num.tryParse(json['count']?.toString() ?? '0') ?? 0).toInt();
    id = (num.tryParse(json['id']?.toString() ?? '0') ?? 0).toInt();
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['open'] = this.open;
    data['close'] = this.close;
    data['high'] = this.high;
    data['low'] = this.low;
    data['vol'] = this.vol;
    data['amount'] = this.amount;
    data['count'] = this.count;
    return data;
  }

  @override
  String toString() {
    return 'MarketModel{open: $open, high: $high, low: $low, close: $close, vol: $vol, id: $id}';
  }
}
